package com.ybwh.springboot1.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ybwh.springboot1.common.ReturnT;
import com.ybwh.springboot1.conf.intercepter.login.LoginRequired;
import com.ybwh.springboot1.model.po.Dict;
import com.ybwh.springboot1.model.vo.DataTabelPageVo;
import com.ybwh.springboot1.service.DictService;

@LoginRequired
@Controller
@RequestMapping("/dict")
public class DictController {

	@Autowired
	private DictService dictService;

	/**
	 * 消息配置页面
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView p_msgConf() {
		ModelAndView mv = new ModelAndView("/dict/list");
		return mv;

	}
	
	@RequestMapping("/pageList")
	@ResponseBody
	public DataTabelPageVo<Dict> pageList(
			@RequestParam(value = "dictGroup", required = false) String dictGroup,
			@RequestParam(value = "isDel", required = false) Integer isDel,
			@RequestParam(required = false, defaultValue = "0") int start,
			@RequestParam(required = false, defaultValue = "10") int length) {
		
		Map<String, Object> param = new HashMap<>();
		if(StringUtils.isNotBlank(dictGroup)) {
			param.put("dictGroup", dictGroup);
		}
		
		if(null != isDel) {
			param.put("isDel", isDel);
		}
		
		DataTabelPageVo<Dict> pageData = dictService.pageList(param,start,length);
		return pageData;

	}

	@RequestMapping("/addOrUpdate")
	@ResponseBody
	public ReturnT<Object> addOrUpdate(@ModelAttribute Dict dict){
		
		try {
			dictService.addOrUpdate(dict);
		} catch (Exception e) {
			return new ReturnT<>(ReturnT.FAIL_CODE,e.getMessage(),null);
		}
		
		return new ReturnT<>(ReturnT.SUCCESS_CODE);
	}
}
